"""
@Version: 1.0
@Project: BeautyReport
@Author: sws
@Data: 2020/7/22 下午15:36
@File: base
@License: MIT
"""
import time

# import win32api
# import win32con
from selenium.webdriver.support.ui import Select #导入select下拉框
from selenium import webdriver

broswer = 'chrome'
path = "D:\python\python\python_ty\chromedriver.exe"
def getDriver():
    if (broswer == 'chrome' or broswer == 'Chrome'):
        driver = webdriver.Chrome(path)
    elif (broswer == 'firefox' or broswer == 'Firefox'):
        driver = webdriver.Firefox(path)
    elif (broswer == 'ie' or broswer == 'Ie'):
        driver = webdriver.Ie(path)
    else:
        driver = webdriver.Edge(path)
    return driver

# 定位元素
def locateElement(self, locator):
    return self.find_element(*locator)

# 退出登陆
def logout(self):
    time.sleep(1)
    self.find_element_by_xpath('//*[@id="userstr"]/a[3]/img').click()

#下面都是方法
#wait_img是对蜂巢系统加载等待时候的那个加载图标进行等待的函数，
# 输入的值有max_time:最多等待的时间，
# position：这个加载图片或按钮的位置,
# css_style_property:是style里面的变量（比如display）
# css_value是具体的css样式的值（比如：block）
#打包前：
def wait_img(self,max_time):
    self.implicitly_wait(10)
    for i in range(0, max_time):
        position = self.find_element_by_class_name('loading')
        loading = position.value_of_css_property('display')
        if loading == 'block':
            time.sleep(1)
        else:
            break
        if i == max_time-1:
            print('输入等待的时间过短，系统太卡了')
# def wait_img(max_time,position,css_style_property,css_value):
#     for i in range(0, max_time):
#         loading = position.value_of_css_property(css_style_property)
#         if loading == css_value:
#             time.sleep(1)
#         else:
#             break
#         if i == max_time-1:
#             print('输入等待的时间过短，系统太卡了,请重新输入一个长的时间')

#这个是登录的窗口，并且判断密码是不是123456
def login(self,username,password):
    self.implicitly_wait(10)
    self.find_element_by_id('LoginId').clear()
    self.find_element_by_id('Securepwd').clear()
    self.find_element_by_id('LoginId').send_keys(username)       #输入账户
    self.find_element_by_id('Securepwd').send_keys(password)  #输入密码
        ####以后要判断是否需要取消更新密码按钮
    self.find_element_by_class_name('sign').click()
    time.sleep(3)
    self.implicitly_wait(3)
    try:
        position = self.find_element_by_class_name('layui-layer-shade')
        loading = position.value_of_css_property('display')
        if password == 123456 or loading == 'block':
            self.find_element_by_css_selector('.layui-layer-btn- > .layui-layer-btn1').click()  # 取消更新密码按钮
    except:
        self.implicitly_wait(10)
        pass
    # elif
    # elif self.find_elements_by_id('layui-layer-shade1'):
    #     self.find_element_by_class_name('layui-layer-btn1').click()

    print(f'·····{username}账号登录成功·····')
#新增酒店功能
def addhotel(self,Hotel_name):
    # #
    # #下面是新增酒店
    #
    Tel = int(time.time()) + 10000000000

    self.implicitly_wait(10)

    self.find_element_by_id('add1').click()  # 点击新增

    self.switch_to.frame(self.find_element_by_xpath(
        '/html/body/div[2]/table/tbody/tr[2]/td[2]/div/table/tbody/tr[2]/td[2]/div/iframe'))  # 进入到小窗口,嵌入式html必须 要去找frame
    self.implicitly_wait(5)
    self.find_element_by_id('HotelName').send_keys(Hotel_name)  # 输入酒店名称
    self.find_element_by_id('HotelOtherName').send_keys(Hotel_name)  # 输入酒店别名
    self.find_element_by_id('HotelBusinessLicense').send_keys(Hotel_name)  # 输入酒店营业执照名称

    select = Select(self.find_element_by_id('StarLevel'))  # 创建select对象
    select.select_by_visible_text('五星')  # 选择五星

    Select(self.find_element_by_id('RoomPriceRange')).select_by_visible_text('100以下')  # 选择起售价
    self.find_element_by_id('RoomCount').send_keys('100')  # 输入房间数量100
    Select(self.find_element_by_id('Province')).select_by_visible_text('四川省')  # 选择了四川省
    Select(self.find_element_by_id('CityId')).select_by_visible_text('成都市')  # 选择了成都市
    Select(self.find_element_by_id('RegionId')).select_by_visible_text('高新区')  # 选择地区
    Select(self.find_element_by_id('TradingAreaId')).select_by_visible_text('春熙路/太古里商业区')  # 选择商圈
    self.find_element_by_id('HotelAddress').send_keys('六中')  # 填写酒店地址
    self.find_element_by_css_selector('[onclick="searchByStationName();"]').click()  # 点击查询
    Select(self.find_element_by_id('HotelTypes')).select_by_visible_text('青年旅社')  # 选择酒店类型
    Select(self.find_element_by_id('CustomerSource')).select_by_visible_text('陌生拜访')  # 选择客户来源
    self.find_element_by_id('Telephone').send_keys(Tel)  # 把时间放进去加个1就有11位了
    self.find_element_by_id('LegalPerson').send_keys('酒店法人姓名')  # 法人姓名
    self.find_element_by_id('DirectorName').send_keys('负责人姓名')  # 负责人姓名
    self.find_element_by_id('DirectorTel').send_keys("13635491602")  # 酒店负责电话

    self.switch_to.default_content()  # 从frame中返回到原来的地方
    time.sleep(1)  # 定位地图的时间有点慢，等一下它
    self.find_element_by_class_name('ui_state_highlight').click()  # 点击确认

    # 注意它加载的不一定是图片，它还有个小圈圈加载，很烦，并不是直接就出现下面的图片加载哈
    time.sleep(1)
    # 我封装的函数，当wd.find_element_by_class_name('loading')中style样式display=block的时候就等待，最多等待20秒
    wait_img(self,20)
    print(f'·····{Hotel_name} 酒店新增成功·····')
    return Tel
#搜索酒店功能:
def hotel_search(self,Hotel_name):
    self.implicitly_wait(10)
    # 我封装的函数，当wd.find_element_by_class_name('loading')中style样式display=block的时候就等待，最多等待20秒
    wait_img(self,20)
    self.find_element_by_id('HotelName').clear()
    self.find_element_by_id('HotelName').send_keys(Hotel_name)
    self.find_element_by_id('search1').click()

    # 我封装的函数，当wd.find_element_by_class_name('loading')中style样式display=block的时候就等待，最多等待20秒
    wait_img(self,20)

#发起签约合同,选择首次付款方式,Month首次付款月份比如：1个月
def initiating_contract(self,contract_first_payment_method,Month):
    self.implicitly_wait(10)
    time.sleep(1)
    self.find_element_by_xpath('//*[@id="dataList"]/tbody/tr[2]/td[8]/a[1]').click()  # 点击编辑
    self.find_elements_by_css_selector('.ui_buttons > input')[1].click()  # 点击业务追踪
    time.sleep(2)  # 什么垃圾啊，非要多等一秒
    self.find_element_by_css_selector('.ui_state_highlight').click()  # 点击新增

    # 进入新增业务追踪
    time.sleep(1)
    self.switch_to.frame(self.find_element_by_xpath(
        '/html/body/div[2]/table/tbody/tr[2]/td[2]/div/table/tbody/tr[2]/td[2]/div/iframe'))  # 进入该页面的iframe
    self.implicitly_wait(5)
    Select(self.find_element_by_id('TraceWay')).select_by_visible_text('面谈')  # 选择跟进方式
    Select(self.find_element_by_id('TraceStatus')).select_by_visible_text('发起签约')  # 选择跟进结果
    self.find_elements_by_tag_name('textarea')[0].send_keys('自动化测试数据')  # 输入信息记录
    self.find_element_by_id('VisitTime').click()  # 点击时间输入框
    time.sleep(1)  # 服了，搞了半天要等一秒，tm的
    self.switch_to.default_content()
    iframes = self.find_elements_by_tag_name('iframe')

    self.switch_to.frame(iframes[3])
    self.implicitly_wait(5)
    self.find_element_by_id('dpTodayInput').click()  # 选择今天

    self.switch_to.default_content()  # 重置iframe
    self.find_elements_by_class_name('ui_state_highlight')[0].click()  # 点击确认
    time.sleep(3)
    self.switch_to.frame(self.find_element_by_xpath(
        '/html/body/div[2]/table/tbody/tr[2]/td[2]/div/table/tbody/tr[2]/td[2]/div/iframe'))  # 进入登记合同页面
    self.find_element_by_id('TaxRegistrationNum').send_keys('91510100399610774D')
    self.find_element_by_id('DirectorName').send_keys('自动化测试签约人姓名')
    self.find_element_by_id('LegalPerson').send_keys('法人姓名')
    self.find_element_by_id('IDCard').send_keys('500000000000000000')
    self.find_element_by_id('DirectorTel').send_keys('11111111111')
    Select(self.find_element_by_id('CustomerSource')).select_by_visible_text('朋友介绍')
    Select(self.find_element_by_id('ChargeModel')).select_by_visible_text('基础服务费加提成')
    Select(self.find_element_by_id('ChargeModelId')).select_by_visible_text('标准P1基础服务费2000+3%')
    #选择首次合同付款方式
    Select(self.find_element_by_id('PayPeriod')).select_by_visible_text(contract_first_payment_method)
    if contract_first_payment_method == '月付':
        Select(self.find_element_by_id('FirstPayMonths')).select_by_visible_text(Month)
    self.switch_to.default_content()
    self.find_element_by_class_name('ui_state_highlight').click()  # 登记合同
    time.sleep(1)
    if Month==None:
        print(f'·····首次付款方式{contract_first_payment_method}合同签约发起成功·····')
    else:
        print(f'·····首次付款{contract_first_payment_method}-{Month}合同签约发起成功·····')
    self.find_element_by_xpath('/html/body/div[2]/table/tbody/tr[2]/td[2]/div/table/tbody/tr[1]/td/div/div[2]/a[4]').click()#关闭编辑酒店

#审核合同
def review_contract(self):
    self.implicitly_wait(10)
    self.find_elements_by_css_selector('#contentpage > table tr')[1].find_elements_by_tag_name('a')[0].click()  # 点击审核
    self.find_element_by_class_name('ui_state_highlight').click()  # 点击审核通过
    time.sleep(1)
    self.find_element_by_xpath(
        '/html/body/div[1]/table/tbody/tr[2]/td[2]/div/table/tbody/tr[3]/td/div/input[1]').click()  # 点击确定

    time.sleep(1)
    # 我封装的函数，当wd.find_element_by_class_name('loading')中style样式display=block的时候就等待，最多等待20秒
    wait_img(self,20)
    print(f'·····合同审核通过·····')

#销售收款登记
def sales_collection_registration(self):
    self.implicitly_wait(10)
    for i in self.find_elements_by_xpath('//*[@id="dataList"]/tbody/tr/td[1]/input'):
        i.click()

    self.find_element_by_id('Handle').click()
    # 进入收款登记窗口
    self.switch_to.frame(self.find_element_by_xpath(
        '/html/body/div[2]/table/tbody/tr[2]/td[2]/div/table/tbody/tr[2]/td[2]/div/iframe'))  # 进入收款登记iframe已优化
    self.implicitly_wait(5)
    Select(self.find_element_by_id('AccountId')).select_by_visible_text('中国光大银行……1289')  # 选择收款账号
    self.find_element_by_id('Remark').send_keys('自动化测试备注')  # 填写备注

    #这里是上传收款凭证的（现在是非必填了我就懒得弄了）
    # self.find_element_by_id('btn_select_file_Files_file').click()
    # self.find_element_by_class_name('input-file').send_keys('D:\头像.png')
    # time.sleep(2)
    #
    # win32api.keybd_event(27, 0, 0, 0)  # 键盘esc弹出
    # win32api.keybd_event(27, 0, win32con.KEYEVENTF_KEYUP, 0)  # 键盘esc松开

    self.switch_to.default_content()
    self.find_element_by_class_name('ui_state_highlight').click()  # 点确定
    time.sleep(2)
    # 我封装的函数，当wd.find_element_by_class_name('loading')中style样式display=block的时候就等待，最多等待20秒
    wait_img(self,20)
    print('·····销售收款登记成功·····')

#财务对销售的收款确定
def collection_confirmation(self):
    self.implicitly_wait(10)
    self.find_element_by_xpath('//*[@id="dataList"]/tbody/tr[2]/td[13]/a[1]').click()  # 点击收款确认
    self.find_element_by_class_name('ui_state_highlight').click()  # 点击确定

    time.sleep(1)
    # 我封装的函数，当wd.find_element_by_class_name('loading')中style样式display=block的时候就等待，最多等待20秒
    wait_img(self,20)
    print('·····财务成功收款确认·····')

#财务对销售的收款拒绝
def collection_rejection(self):
    self.implicitly_wait(10)
    self.find_element_by_xpath('//*[@id="dataList"]/tbody/tr[2]/td[13]/a[2]').click()  # 点击收款拒绝
    self.find_element_by_class_name('layui-layer-input').click()  # 点击这个文本框
    self.find_element_by_class_name('layui-layer-input').send_keys('自动化测试销售拒绝')#输入拒绝原因
    self.find_element_by_class_name('layui-layer-btn0').click()#点击确认

    time.sleep(1)
    # 我封装的函数，当wd.find_element_by_class_name('loading')中style样式display=block的时候就等待，最多等待20秒
    wait_img(self, 20)
    print('·····财务成功拒绝收款·····')

#财务对运营的收款确定
def collection_confirmation2(self):
    self.implicitly_wait(10)
    self.find_element_by_xpath('//*[@id="dataList"]/tbody/tr[2]/td[13]/a[1]').click()  # 点击收款确认
    self.find_element_by_class_name('ui_state_highlight').click()  # 点击确定

    time.sleep(1)
    # 我封装的函数，当wd.find_element_by_class_name('loading')中style样式display=block的时候就等待，最多等待20秒
    wait_img(self,20)
    print('·····财务成功收款确认·····')

#安排运营人员
def arrange_personnel(self,his_name):
    self.implicitly_wait(10)
    self.find_element_by_xpath('//*[@id="dataList"]/tbody/tr[2]/td[8]/a').click()  # 点击设置运营人员
    self.find_element_by_class_name('ui_state_highlight').click()  # 点击新增
    time.sleep(1)  ##下面的iframe定位有点不准所以等了一秒，以后有时间要优化这里
    self.switch_to.frame(self.find_element_by_xpath(
        '/html/body/div[2]/table/tbody/tr[2]/td[2]/div/table/tbody/tr[2]/td[2]/div/iframe'))  # 进入新增酒店运营人员iframe
    self.implicitly_wait(5)
    Select(self.find_element_by_id('BusinessStaffId')).select_by_visible_text(his_name)  # 选择运营人员
    self.switch_to.default_content()
    self.find_element_by_xpath(
        '/html/body/div[2]/table/tbody/tr[2]/td[2]/div/table/tbody/tr[3]/td/div/input[1]').click()  # 点击确定
    time.sleep(1)
    # 我封装的函数，当wd.find_element_by_class_name('loading')中style样式display=block的时候就等待，最多等待20秒
    wait_img(self,20)
    print(f'·····{his_name}运营人员安排成功·····')

#运营续费登记num:输入续费登记几个月
def renewal_collection_registration(self,num):
    self.implicitly_wait(10)
    # 这是续费付款num个月的操作
    for i in range(0, int(num)):
        self.find_elements_by_xpath('//*[@id="dataList"]/tbody/tr/td[1]/input')[i].click()

    # 点击批量登记
    self.find_element_by_id('Handle').click()

    self.switch_to.frame(self.find_element_by_xpath(
        '/html/body/div[2]/table/tbody/tr[2]/td[2]/div/table/tbody/tr[2]/td[2]/div/iframe'))  # 进入iframe
    self.implicitly_wait(5)
    Select(
        self.find_element_by_id('AccountId')).select_by_visible_text('中国光大银行……1289')
    self.find_element_by_id('Remark').send_keys('测试备注')

    #上传图片现在是非必填了
    # self.find_element_by_id('btn_select_file_Files_file').click()  # 上传图片
    # self.find_element_by_class_name('input-file').send_keys('D:\头像.png')
    # time.sleep(2)
    # win32api.keybd_event(27, 0, 0, 0)  # 键盘esc弹出
    # win32api.keybd_event(27, 0, win32con.KEYEVENTF_KEYUP, 0)  # 键盘esc松开

    self.switch_to.default_content()
    self.find_element_by_xpath(
        '/html/body/div[2]/table/tbody/tr[2]/td[2]/div/table/tbody/tr[3]/td/div/input[1]').click()  # 确定登记
    time.sleep(1)
    # 我封装的函数，当wd.find_element_by_class_name('loading')中style样式display=block的时候就等待，最多等待20秒
    wait_img(self,20)
    print(f'·····运营人员成功续费 {num} 个月')

#新增账户
def add(self,LoginId,Securepwd,BranchId,Role,DirectDepartmentId,Name,num):
    self.find_element_by_id('add1').click()
    time.sleep(1)
    # 进入iframe
    self.switch_to.frame(self.find_element_by_xpath(
        '/html/body/div[2]/table/tbody/tr[2]/td[2]/div/table/tbody/tr[2]/td[2]/div/iframe'))
    self.find_element_by_id('LoginId').send_keys(LoginId)#账户
    self.find_element_by_id('Securepwd').send_keys(Securepwd)#密码
    self.find_element_by_id('SecurepwdRepeat').send_keys(Securepwd)#确认密码

    Select(self.find_element_by_id('BranchId')).select_by_visible_text(BranchId)#平台
    Select(self.find_element_by_id('Role')).select_by_visible_text(Role)#角色
    Select(self.find_element_by_id('DirectDepartmentId')).select_by_visible_text(DirectDepartmentId)#直属部门
    self.find_element_by_id('Name').send_keys(Name)#名字
    self.find_element_by_id('Mobile').send_keys('13635491601')#电话
    #部门
    self.find_element_by_xpath('//*[@id="myform"]/table/tbody/tr[6]/td/input[2]').click()
    time.sleep(1)
    # 进入iframe选择部门
    self.switch_to.default_content()
    self.switch_to.frame(self.find_element_by_xpath('/html/body/div[2]/table/tbody/tr[2]/td[2]/div/table/tbody/tr[2]/td[2]/div/iframe'))
    self.find_element_by_css_selector(
        f".tree input[value='{num}']").click()

    self.switch_to.default_content()
    #点击确定
    self.find_element_by_class_name('ui_state_highlight').click()
    # 点击确定
    self.find_element_by_class_name('ui_state_highlight').click()
    time.sleep(1)
    wait_img(self, 5)
    self.find_element_by_id('userName').clear()
    self.find_element_by_id('userName').send_keys(LoginId)
    self.find_element_by_id('search1').click()
    wait_img(self,5)
